package com.bsj.media.media;


import io.netty.channel.Channel;
import lombok.extern.slf4j.Slf4j;


@Slf4j
public abstract class mediaStream {

    protected boolean isClosed; // 是否关闭
    protected String videoType; // 音视频类型, video ,audio , media
    protected int audioCode;
    protected int flags = 0; // 使用标志，未使用：0，正在使用：1，结束：2
    protected boolean isFirstSend = true; // 是否第一次转发数据给前端，是第一次转发时通知命令请求成功

    protected int width; // 视频宽
    protected int height; // 视频高
    protected int videoRate; //帧率

    protected int newFDProtocolDataLength = 19; //FDFD协议数据的数据体，19字节+body
    private int proctolLimit = 21; //FDFD协议数据的数据体+校验1+结束位1 (新协议)
    private int oldFDProtocolDataLength = 16; //老FD协议中的数据体长度

    /**
     * 视频帧数，用于直接下载回放，作为结束回放下载标识
     */
    public int videoFrameNum;

    public mediaStream() {
    }

    public mediaStream(String videoType, String uri, Channel sockChn) {
        this.videoType = videoType;
    }

    public int getFlags() {
        return flags;
    }

    public void setFlags(int flags) {
        this.flags = flags;
    }

    /**
     * 打包数据
     *
     * @param data
     * @param offset
     * @param len
     * @return
     */
    public abstract int PacketData(byte[] data, int offset, int len, byte[] dstBuffer);

    public boolean isClosed() {
        return isClosed;
    }

    public void setClosed(boolean isClosed) {
        this.isClosed = isClosed;
    }

    /**
     * 分辨率， 详见协议
     *
     * @param solution
     */
    public void getSolutionRatio(int solution) {
        switch (solution) {
            case 0:
                width = 176;
                height = 144;
                break;
            case 2:
                width = 704;
                height = 288;
                break;
            case 3:
                width = 704;
                height = 576;
                break;
            case 4:
                width = 1280;
                height = 720;
                break;
            case 5:
                width = 1280;
                height = 960;
                break;
            case 6:
                width = 1920;
                height = 1080;
                break;
            case 7:
                width = 2560;
                height = 1440;
                break;
            case 127:
                width = 960;
                height = 540;
                break;
            default:
                width = 352;
                height = 288;
                break;
        }
    }

    /**
     * 获取封装文件类型， MEDIA 1是音视频，VIDEO 2是视频，AUDIO 3是音频
     *
     * @return
     */
    public int getVideoType() {
        int flvType = 0;
        switch (this.videoType) {
            case VideoServerCmdType.AUDIO:
            case VideoServerCmdType.SOUND:
            case VideoServerCmdType.RecAudio:
                flvType = 3;
                break;
            case VideoServerCmdType.VIDEO:
            case VideoServerCmdType.RecVideo:
                flvType = 2;
                break;
            case VideoServerCmdType.MEDIA:
            case VideoServerCmdType.FLV:
                flvType = 1;
                break;
            default:
                break;
        }

        return flvType;
    }

    /**
     * <p>
     *     从FD协议数据中，
     *     通过协议中的数据体的位置，位移获取数据
     * </p>
     * **/
    public byte[] getData(byte[] protocolBody) {
        int bodyLength = protocolBody.length - proctolLimit;
        //拷贝的接收返回字节数组
        byte[] body = new byte[bodyLength];
        //拷贝方式
        System.arraycopy(protocolBody, newFDProtocolDataLength, body, 0, bodyLength);
        return body;
    }

    public void setVideoType(String videoType) {
        this.videoType = videoType;
    }

    public int getAudioCode() {
        return audioCode;
    }

    public void setAudioCode(int audioCode) {
        this.audioCode = audioCode;
    }


    public int getVideoRate() {
        return videoRate;
    }

    public void setVideoRate(int videoRate) {
        this.videoRate = videoRate;
    }

    public int getNewFDProtocolDataLength() {
        return newFDProtocolDataLength;
    }

    public void setNewFDProtocolDataLength(int newFDProtocolDataLength) {
        this.newFDProtocolDataLength = newFDProtocolDataLength;
    }

    public int getProctolLimit() {
        return proctolLimit;
    }

    public void setProctolLimit(int proctolLimit) {
        this.proctolLimit = proctolLimit;
    }

    public int getOldFDProtocolDataLength() {
        return oldFDProtocolDataLength;
    }

    public void setOldFDProtocolDataLength(int oldFDProtocolDataLength) {
        this.oldFDProtocolDataLength = oldFDProtocolDataLength;
    }
}
